/**
 * @author Linrui
 * @since 2011-05-06
 * 校园  控制层类
 */
package com.qeado.colleage.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.qeado.colleage.model.Colleage;
import com.qeado.colleage.service.ColleageService;
import com.qeado.news.model.News;
import com.qeado.news.service.NewsService;
import com.qeado.sort.model.Sort;
import com.qeado.sort.service.SortService;
import com.qeado.util.PageHelper;

@Scope("session")
@Controller
public class ColleageAction extends ActionSupport {

	@Autowired
	private NewsService  newsService;
	@Autowired
	private SortService  sortService;
	@Autowired
	private ColleageService colleageService;
	
	private News news ;
	private Colleage school; // 校园DTO
	
	private PageHelper colleageInfo; // 校园资讯
	private PageHelper technologyInfo; // 科技前沿
	private PageHelper colleageNeed; // 校园需求信息
	private PageHelper compyNeed; // 企业需求信息
	private PageHelper newsList; // 信息列表
	private PageHelper rankNews; // 排行榜
	private PageHelper schoolList; // 校园列表

	private final String CREATE_DATE = "create_date";
	private final String ASC = "desc";
	private final String P10 = "10";
	private final String N1 = "1";
	 
	private String pageNum = N1;
	private String pageSize = P10;
	private String typeCode = null;
	private String orderBy = CREATE_DATE;
	private String asc = ASC;
		
	private Map<String,Sort> schoolTypeList ;
	
	public Map<String,Sort> getSchoolTypeList() {
		return schoolTypeList;
	}

	public Colleage getSchool() {
		return school;
	}

	public void setSchool(Colleage school) {
		this.school = school;
	}

	public String getTypeCode() {
		return typeCode;
	}

	public void setTypeCode(String typeCode) {
		this.typeCode = typeCode;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getAsc() {
		return asc;
	}

	public void setAsc(String asc) {
		this.asc = asc;
	}

	public PageHelper getSchoolList() {
		return schoolList;
	}

	public PageHelper getNewsList() {
		return newsList;
	}

	public void setNewsList(PageHelper newsList) {
		this.newsList = newsList;
	}

	public PageHelper getRankNews() {
		return rankNews;
	}

	public void setRankNews(PageHelper rankNews) {
		this.rankNews = rankNews;
	}

	public String getPageNum() {
		return pageNum;
	}

	public void setPageNum(String pageNum) {
		this.pageNum = pageNum;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	private List<Sort> sortList; // 信息类别
	
	public List<Sort> getSortList() {
		return sortList;
	}

	public void setSortList(List<Sort> sortList) {
		this.sortList = sortList;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}


	public PageHelper getColleageInfo() {
		return colleageInfo;
	}


	public void setColleageInfo(PageHelper colleageInfo) {
		this.colleageInfo = colleageInfo;
	}


	public PageHelper getTechnologyInfo() {
		return technologyInfo;
	}


	public void setTechnologyInfo(PageHelper technologyInfo) {
		this.technologyInfo = technologyInfo;
	}


	public PageHelper getColleageNeed() {
		return colleageNeed;
	}


	public void setColleageNeed(PageHelper colleageNeed) {
		this.colleageNeed = colleageNeed;
	}


	public PageHelper getCompyNeed() {
		return compyNeed;
	}


	public void setCompyNeed(PageHelper compyNeed) {
		this.compyNeed = compyNeed;
	}


	@Override
	public String execute() throws Exception {
		pageNum = N1;
		pageSize = P10;
		colleageInfo = newsService.listBySortByPage(Integer.parseInt(pageNum), "SH001004"); // 加载‘校园资讯’这一类比的信息
		technologyInfo = newsService.listBySortByPage(Integer.parseInt(pageNum), "SH001005"); // 加载‘科技前沿’这一类比的信息
		colleageNeed = newsService.listBySortByPage(Integer.parseInt(pageNum), "SH001006"); // 加载‘院校需求’这一类比的信息
		compyNeed = newsService.listBySortByPage(Integer.parseInt(pageNum), "CM001002"); // 加载‘企业需求’这一类比的信息
		return super.execute();
	}
	
	/* 预先加载类别信息 */
	public String publicInfoPage() throws Exception {
		if( sortList == null ) 
				sortList = sortService.listByPcode("SH001") ;
		return "public_colleage_info";
	}
	
	public String listByType() throws Exception {
		String typeCode = ServletActionContext.getRequest().getParameter("typeCode");
		newsList = newsService.listBySortByPage(Integer.parseInt(pageNum), typeCode);
		if(rankNews == null )
			rankNews = newsService.listHotByType(1 , 10, "SH"); // 查询排行榜
		newsList.setPagebar("listByType");
		return "list";
	}
	
	public String view() throws Exception{
		String id = ServletActionContext.getRequest().getParameter("id");
		news = newsService.queryById(id);
		/* 更新排行榜 */
		if(rankNews == null )
			rankNews = newsService.listHotByType(1 , 10, "SH"); 
		return "view";
	}
	
	public String latestNews() throws Exception {
		newsList = newsService.listByTypeLike(Integer.parseInt(pageNum), Integer.parseInt(pageSize) , "SH", "create_date", "desc");
		if(rankNews == null )
			rankNews = newsService.listHotByType(1 , 10, "SH"); // 查询排行榜
		newsList.setPagebar("latestNews");
		return "list";
	}
	
//	public String listRank() throws Exception {	
//		rankNews = newsService.listHotByType(Integer.parseInt(pageNum), Integer.parseInt(pageSize), "SH"); // 查询排行榜			
//		return "list";
//	}
	
	public String listColleage() {
		if( schoolTypeList == null ) { 
			schoolTypeList = new HashMap<String, Sort>();
			List<Sort> list = sortService.listByPcode("SH003");
			for(Iterator iter = list.iterator(); iter.hasNext(); ){
				Sort sort = (Sort)iter.next();
				schoolTypeList.put(sort.getTypeCode(), sort);
			}
		}
		schoolList = colleageService.listAll(Integer.parseInt(pageNum), Integer.parseInt(pageSize), typeCode, orderBy, asc);
		if( schoolList == null )
			return "visit_fail";
		else {
			schoolList.setPagebar("listColleage");
			return "list";
		}
	}
	
	/* 按关键字搜索 */
	public String search() throws Exception {
		String key = ServletActionContext.getRequest().getParameter("key");
		String type = ServletActionContext.getRequest().getParameter("type");
		String level = ServletActionContext.getRequest().getParameter("level");
		schoolList = colleageService.listByKey(Integer.parseInt(pageNum), Integer.parseInt(pageSize), type , orderBy, asc , key , level);
		schoolList.setPagebar("${ctx}/product/search?key="+key+"&type="+type+"&level="+level);
		return "list";
	}
}
